#include "Core/Kernel.h"

#include "Core/LogService.h"
#include "Core/FileService.h"
#include "GfxDriver/GfxDriver.h"
#include "Image/ImageService.h"

#include <cstdlib>
#include <cassert>

namespace Orca {

	//--------------------------------------------------------------------------
	Kernel *Kernel::msInst = NULL;

	//--------------------------------------------------------------------------
	Kernel* Kernel::inst() {

		assert(msInst);
		return msInst;
	}
	
	//--------------------------------------------------------------------------
	void Kernel::registerDefaultModules() {

		registerService<LogService>	( _createLogService() );
		registerService<FileService>( _createFileService() );
		registerService<GfxDriver>	( _createGfxDriver() );
		registerService<ImageService>( new ImageService() );
	}

	//--------------------------------------------------------------------------
	bool Kernel::startup() {

		ModuleIter curr = mModules.begin();
		ModuleIter end = mModules.end();

		while(curr != end) {

			if( !curr->second->startup() ) {

				//return false;
			}

			++curr;
		}

		return true;
	}

	//--------------------------------------------------------------------------
	void Kernel::step() {

		ModuleIter curr = mModules.begin();
		ModuleIter end = mModules.end();

		while(curr != end) {

			curr->second->step(0.0f);
			++curr;
		}
	}

	//--------------------------------------------------------------------------
	void Kernel::shutdown() {

		ModuleIter curr = mModules.begin();
		ModuleIter end = mModules.end();

		while(curr != end) {

			curr->second->shutdown();
			delete curr->second;

			++curr;
		}
	}

	//--------------------------------------------------------------------------
}